<?php
/***
	jValidation-1.0.php
	name:xiaojia
	email:iatt@qq.com
	qq:273142650
	time:2012.1.3 12:10
***/

	class jValidation
	{
		
		protected $data  = array();
		
		protected $image = NULL;
		
		protected $code  = NULL;
		
		public function init ($data = array())
		{
			
			empty($data['fontsize']) && $data['fontsize'] = 15;
			
			empty($data['how']) && $data['how'] = 4;
			
			empty($data['ttf']) && $data['ttf'] = NULL;
			
			$data['width'] = $data['how'] * $data['fontsize'] * 1.5;
			
			$data['height'] = $data['fontsize'] * 2.5;
			
			$this -> data = $data;
			
			$this -> valGetChar();
			
			return $this -> code;
			
		}
		
		public function getImage ()
		{
			
			$this -> valHeader();
			
			$this -> valImageCreate();
			
			$this -> valImageChar();
			
			$this -> valInterference();
			
			imagepng($this -> image);
			
			imagedestroy($this -> image);
			
		}
		
		protected function valHeader ()
		{
			
			header("Content-type: image/png");
			
		}
		
		protected function valImageCreate ()
		{
			
			$this -> image = imagecreate($this -> data['width'], $this -> data['height']);
			
			imagefill($this -> image, 0, 0, imagecolorallocate($this -> image, 255, 255, 255));
			
		}
		
		protected function valImageChar ()
		{
			
			$left = 0;
			
			$fontsize = $this -> data['fontsize'];
			
			if ($this -> data['ttf'] == NULL) {
				
				for ($i = 0; $i < $this -> data['how']; $i++) {
					
					$left += rand((int)$fontsize * 0.5, (int)$fontsize * 1.5);
					
					$top = rand(0, $fontsize * 1.5);
					
					$color = $this -> valGetColor();
					
					$char = substr($this -> code, $i, 1);
					
					imagechar($this -> image, 5, $left, $top, $char, $color);
					
					$this -> code .= $char;
				}
				
			} else {
			
				for ($i = 0; $i < $this -> data['how']; $i++) {
					
					$left += rand((int)$fontsize * 0.5, (int)$fontsize * 1.5);
					
					$top = rand($fontsize, $fontsize * 2);
					
					$angle = rand(-10, 10);
					
					$color = $this -> valGetColor();
					
					$char = substr($this -> code, $i, 1);
					
					imagettftext($this -> image, $fontsize, $angle, $left, $top, $color, $this -> data['ttf'], $char);
					
					$this -> code .= $char;
				}
			
			}
		
		}
		
		protected function valGetChar ()
		{
			
			$this -> code = NULL;
			
			for ($i = 0; $i < $this -> data['how']; $i++) {
				
				if (rand(0, 1)) {
				
					if (rand(0, 1)) {
						
						$char = chr(rand(65, 90));
						
					} else {
					
						$char = chr(rand(97, 122));
						
					}
					
				} else {
					
					$char = chr(rand(48, 57));
					
				}
				
				$this -> code .= $char;
				
			}
			
		}
		
		protected function valInterference ()
		{
			
			for ($i = 0; $i < $this -> data['how']; $i++) {
				
				$color = $this -> valGetColor();
				
				imagearc($this -> image, rand(-5, $this -> data['width']), rand(-5, $this -> data['height']), rand(20,300), rand(20,200), 55, 44, $color);
				
			}
			
			for ($i = 0; $i < $this -> data['how'] * 40; $i++) {
				
				$color = $this -> valGetColor();
				
				imagesetpixel($this -> image, rand(0, $this -> data['width']), rand(0, $this -> data['height']), $color);
				
			}
			
		}
		
		protected function valGetColor ()
		{
			
			return imagecolorallocate($this -> image, rand(0, 255), rand(0, 255), rand(0, 255));
			
		}
	
	}

?>